Spree::Sample.load_sample("products")
Spree::Sample.load_sample("variants")

products = {}
products[:ror_tote] = Spree::Product.find_by_name!("Ruby on Rails Tote")


def image(name, type="jpeg")
  images_path = Pathname.new(File.dirname(__FILE__)) + "images"
  path = images_path + "#{name}.#{type}"
  return false if !File.exist?(path)
  File.open(path)
end

images = {
  products[:ror_tote].master => [
    {
      :attachment => image("ror_tote")
    },
    {
      :attachment => image("ror_tote_back") 
    }
  ],
}

# products[:ror_baseball_jersey].variants.each do |variant|
#   color = variant.option_value("tshirt-color").downcase
#   main_image = image("ror_baseball_jersey_#{color}", "png")
#   variant.images.create!(:attachment => main_image)
#   back_image = image("ror_baseball_jersey_back_#{color}", "png")
#   if back_image
#     variant.images.create!(:attachment => back_image)
#   end
# end

images.each do |variant, attachments|
  puts "Loading images for #{variant.product.name}"
  attachments.each do |attachment|
    variant.images.create!(attachment)
  end
end

